package com.lzx.resumeweb.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.resumebase.model.Comment;
import com.lzx.resumebase.model.Result;
import com.lzx.resumebase.util.ResultUtil;
import com.lzx.resumebase.util.UUIDUtil;
import com.lzx.resumeservice.service.CommentService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * @ClassName CommentApi
 * @Author 刘正星
 * @Date 2021/6/11 下午1:39
 * @Description
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentApi {

    @Autowired
    private CommentService commentService;

    @PostMapping("/save")
    public Result save(@RequestBody Comment comment){
        if (null==comment){
            log.error("comment is null");
            return ResultUtil.paramError();
        }
        if (!StringUtils.hasText(comment.getId())){
            comment.setId(UUIDUtil.getUUID());
            comment.setGmtCreated(LocalDateTime.now());
        }
        return ResultUtil.success(commentService.save(comment));
    }

    @GetMapping("/list")
    public Result list(){
        return ResultUtil.success(commentService.lambdaQuery().orderByDesc(Comment::getGmtCreated).list());
    }
}
